﻿#include<iostream>
using namespace std;
void up(int child)
{
	int parent = child / 2;
	while (parent >= 1 && head[child] >= head[parent])
	{
		swap(head[child], head[parent]);
		child = parent;
		parent = child / 2;
	}
}

int n;
int head[N];
void down(int parent)
{
	int child = parent * 2;
	while (child <= n)
	{
		if (head[child]<head[child + 1] && head[child + 1]>head[parent])
		{
			child++;
		}
		if (head[child] <= head[parent])
		{
			return;
		}
		swap(head[parent], head[child]);
		parent = child;
		child = parent * 2;

	}
}
const int N = 1e6 + 10;
int n;//标记堆的大小
int heap[N];

void push(int x)
{
	heap[++n] = x;
	up(n);
}


void pop()
{
	swap(heap[1], heap[n]);
	n--;
	down(1);
}

int top()
{
	return heap[1];
}

int size()
{
	return n;
}
int main()
{
	// 测试堆
	int a[10] = { 1, 41, 23, 10, 11, 2, -1, 99, 14, 0 };
	// 将这 10 个数依次放⼊堆中
	for (int i = 0; i < 10; i++)
	{
		push(a[i]);
	}
	while (size())
	{
		cout << top() << " ";
		pop();
	}
	return 0;
}